package com.easemob.chat;

import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.util.Pair;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EMLog;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class EMMessage
  implements Parcelable, Cloneable
{
  private static final String TAG = "msg";
  Type type;
  public Direct direct;
  public Status status = Status.CREATE;
  EMContact from;
  EMContact to;
  MessageBody body;
  String msgId;
  public boolean isAcked = false;
  public boolean isDelivered = false;
  long msgTime;
  ChatType chatType = ChatType.Chat;
  public transient int progress = 0;
  Hashtable<String, Object> attributes = new Hashtable();
  transient boolean unread = true;
  transient boolean offline = false;
  boolean isListened;
  static final String ATTR_ENCRYPTED = "isencrypted";
  public static final Parcelable.Creator<EMMessage> CREATOR = new Parcelable.Creator()
  {
    public EMMessage createFromParcel(Parcel paramAnonymousParcel)
    {
      return new EMMessage(paramAnonymousParcel, null);
    }

    public EMMessage[] newArray(int paramAnonymousInt)
    {
      return new EMMessage[paramAnonymousInt];
    }
  };

  EMMessage(Type paramType)
  {
    this.type = paramType;
    this.msgTime = System.currentTimeMillis();
  }

  public Type getType()
  {
    return this.type;
  }

  public MessageBody getBody()
  {
    return this.body;
  }

  public long getMsgTime()
  {
    return this.msgTime;
  }

  public void setMsgTime(long paramLong)
  {
    this.msgTime = paramLong;
  }

  public static EMMessage createSendMessage(Type paramType)
  {
    EMMessage localEMMessage = new EMMessage(paramType);
    localEMMessage.direct = Direct.SEND;
    EMContact localEMContact = EMSessionManager.getInstance().currentUser;
    if (localEMContact == null)
    {
      String str = EMSessionManager.getInstance().getLastLoginUser();
      localEMContact = EMContactManager.getInstance().getContactByUserName(str);
    }
    localEMMessage.from = localEMContact;
    localEMMessage.setMsgId(EMMessageUtils.getUniqueMessageId());
    return localEMMessage;
  }

  public static EMMessage createReceiveMessage(Type paramType)
  {
    EMMessage localEMMessage = new EMMessage(paramType);
    localEMMessage.direct = Direct.RECEIVE;
    localEMMessage.to = EMSessionManager.getInstance().currentUser;
    return localEMMessage;
  }

  public void addBody(MessageBody paramMessageBody)
  {
    this.body = paramMessageBody;
  }

  public String getFrom()
  {
    if (this.from == null)
      return null;
    return this.from.username;
  }

  public void setFrom(String paramString)
  {
    EMContact localEMContact = new EMContact();
    localEMContact.setUsername(paramString);
    this.from = localEMContact;
  }

  public void setTo(String paramString)
  {
    EMContact localEMContact = new EMContact();
    localEMContact.setUsername(paramString);
    this.to = localEMContact;
  }

  public String getTo()
  {
    if (this.to == null)
      return null;
    return this.to.username;
  }

  public String getMsgId()
  {
    return this.msgId;
  }

  public void setMsgId(String paramString)
  {
    this.msgId = paramString;
  }

  public void setReceipt(String paramString)
  {
    EMContactManager localEMContactManager = EMContactManager.getInstance();
    EMContact localEMContact = null;
    if (paramString.contains("@"))
      EMLog.e("msg", "error wrong uesrname format:" + paramString);
    else
      localEMContact = localEMContactManager.getContactByUserName(paramString);
    if (localEMContact == null)
      localEMContact = new EMContact(paramString);
    this.to = localEMContact;
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("msg{from:" + this.from.username);
    localStringBuffer.append(", to:" + this.to.username);
    localStringBuffer.append(" body:" + this.body.toString());
    return localStringBuffer.toString();
  }

  public void setAttribute(String paramString, boolean paramBoolean)
  {
    if (this.attributes == null)
      this.attributes = new Hashtable();
    this.attributes.put(paramString, new Boolean(paramBoolean));
  }

  public void setAttribute(String paramString, int paramInt)
  {
    if (this.attributes == null)
      this.attributes = new Hashtable();
    this.attributes.put(paramString, new Integer(paramInt));
  }

  public void setAttribute(String paramString, JSONObject paramJSONObject)
  {
    if (this.attributes == null)
      this.attributes = new Hashtable();
    this.attributes.put(paramString, paramJSONObject);
  }

  public void setAttribute(String paramString, JSONArray paramJSONArray)
  {
    if (this.attributes == null)
      this.attributes = new Hashtable();
    this.attributes.put(paramString, paramJSONArray);
  }

  public void setAttribute(String paramString1, String paramString2)
  {
    if (this.attributes == null)
      this.attributes = new Hashtable();
    this.attributes.put(paramString1, paramString2);
  }

  public boolean getBooleanAttribute(String paramString)
    throws EaseMobException
  {
    Boolean localBoolean = null;
    if (this.attributes != null)
    {
      Object localObject = this.attributes.get(paramString);
      if ((localObject instanceof Boolean))
      {
        localBoolean = (Boolean)localObject;
      }
      else if ((localObject instanceof Integer))
      {
        int i = ((Integer)localObject).intValue();
        if (i > 0)
          localBoolean = Boolean.valueOf(true);
        else
          localBoolean = Boolean.valueOf(false);
      }
    }
    if (localBoolean == null)
      throw new EaseMobException("attribute " + paramString + " not found");
    return localBoolean.booleanValue();
  }

  public boolean getBooleanAttribute(String paramString, boolean paramBoolean)
  {
    if (this.attributes == null)
      return paramBoolean;
    Boolean localBoolean = null;
    try
    {
      localBoolean = Boolean.valueOf(getBooleanAttribute(paramString));
    }
    catch (EaseMobException localEaseMobException)
    {
    }
    if (localBoolean == null)
      return paramBoolean;
    return localBoolean.booleanValue();
  }

  public int getIntAttribute(String paramString, int paramInt)
  {
    Integer localInteger = null;
    if (this.attributes != null)
      localInteger = (Integer)this.attributes.get(paramString);
    if (localInteger == null)
      return paramInt;
    return localInteger.intValue();
  }

  public int getIntAttribute(String paramString)
    throws EaseMobException
  {
    Integer localInteger = null;
    if (this.attributes != null)
      localInteger = (Integer)this.attributes.get(paramString);
    if (localInteger == null)
      throw new EaseMobException("attribute " + paramString + " not found");
    return localInteger.intValue();
  }

  public String getStringAttribute(String paramString)
    throws EaseMobException
  {
    if ((this.attributes != null) && (this.attributes.containsKey(paramString)))
    {
      Object localObject = this.attributes.get(paramString);
      if ((localObject instanceof String))
        return (String)localObject;
      if ((localObject instanceof JSONObject))
        return ((JSONObject)localObject).toString();
      if ((localObject instanceof JSONArray))
        return ((JSONArray)localObject).toString();
      throw new EaseMobException("attribute " + paramString + " not String type");
    }
    throw new EaseMobException("attribute " + paramString + " not found");
  }

  public String getStringAttribute(String paramString1, String paramString2)
  {
    if ((this.attributes != null) && (this.attributes.containsKey(paramString1)))
    {
      Object localObject = this.attributes.get(paramString1);
      if ((localObject instanceof String))
        return (String)localObject;
      if ((localObject instanceof JSONObject))
        return ((JSONObject)localObject).toString();
      if ((localObject instanceof JSONArray))
        return ((JSONArray)localObject).toString();
    }
    return paramString2;
  }

  public JSONObject getJSONObjectAttribute(String paramString)
    throws EaseMobException
  {
    if ((this.attributes != null) && (this.attributes.containsKey(paramString)))
    {
      Object localObject = this.attributes.get(paramString);
      if ((localObject instanceof JSONObject))
        return (JSONObject)localObject;
      if ((localObject instanceof String))
        try
        {
          JSONObject localJSONObject = new JSONObject((String)localObject);
          return localJSONObject;
        }
        catch (JSONException localJSONException)
        {
        }
      throw new EaseMobException("attribute " + paramString + " not JSONObject type");
    }
    throw new EaseMobException("attribute " + paramString + " not found");
  }

  public JSONArray getJSONArrayAttribute(String paramString)
    throws EaseMobException
  {
    if ((this.attributes != null) && (this.attributes.containsKey(paramString)))
    {
      Object localObject = this.attributes.get(paramString);
      if ((localObject instanceof JSONArray))
        return (JSONArray)localObject;
      if ((localObject instanceof String))
        try
        {
          JSONArray localJSONArray = new JSONArray((String)localObject);
          return localJSONArray;
        }
        catch (JSONException localJSONException)
        {
        }
      throw new EaseMobException("attribute " + paramString + " not JSONArray type");
    }
    throw new EaseMobException("attribute " + paramString + " not found");
  }

  public ChatType getChatType()
  {
    return this.chatType;
  }

  public void setChatType(ChatType paramChatType)
  {
    this.chatType = paramChatType;
  }

  public int describeContents()
  {
    return 0;
  }

  public void writeToParcel(Parcel paramParcel, int paramInt)
  {
    paramParcel.writeString(this.type.name());
    paramParcel.writeString(this.direct.name());
    paramParcel.writeString(this.msgId);
    paramParcel.writeLong(this.msgTime);
    ArrayList localArrayList1 = new ArrayList();
    ArrayList localArrayList2 = new ArrayList();
    Hashtable localHashtable = new Hashtable();
    Iterator localIterator = this.attributes.entrySet().iterator();
    Object localObject;
    while (localIterator.hasNext())
    {
      localObject = (Map.Entry)localIterator.next();
      if (((Map.Entry)localObject).getValue() != null)
        if ((((Map.Entry)localObject).getValue() instanceof JSONObject))
          localArrayList1.add(Pair.create((String)((Map.Entry)localObject).getKey(), (JSONObject)((Map.Entry)localObject).getValue()));
        else if ((((Map.Entry)localObject).getValue() instanceof JSONArray))
          localArrayList2.add(Pair.create((String)((Map.Entry)localObject).getKey(), (JSONArray)((Map.Entry)localObject).getValue()));
        else
          localHashtable.put((String)((Map.Entry)localObject).getKey(), ((Map.Entry)localObject).getValue());
    }
    paramParcel.writeInt(localArrayList1.size());
    localIterator = localArrayList1.iterator();
    while (localIterator.hasNext())
    {
      localObject = (Pair)localIterator.next();
      paramParcel.writeString((String)((Pair)localObject).first);
      paramParcel.writeString(((JSONObject)((Pair)localObject).second).toString());
    }
    paramParcel.writeInt(localArrayList2.size());
    localIterator = localArrayList2.iterator();
    while (localIterator.hasNext())
    {
      localObject = (Pair)localIterator.next();
      paramParcel.writeString((String)((Pair)localObject).first);
      paramParcel.writeString(((JSONArray)((Pair)localObject).second).toString());
    }
    paramParcel.writeMap(localHashtable);
    paramParcel.writeParcelable(this.from, paramInt);
    paramParcel.writeParcelable(this.to, paramInt);
    paramParcel.writeParcelable(this.body, paramInt);
    paramParcel.writeString(this.chatType.name());
  }

  private EMMessage(Parcel paramParcel)
  {
    this.type = Type.valueOf(paramParcel.readString());
    this.direct = Direct.valueOf(paramParcel.readString());
    this.msgId = paramParcel.readString();
    this.msgTime = paramParcel.readLong();
    this.attributes = new Hashtable();
    int i = paramParcel.readInt();
    Object localObject;
    for (int j = 0; j < i; j++)
    {
      String str = paramParcel.readString();
      localObject = null;
      try
      {
        localObject = new JSONObject(paramParcel.readString());
      }
      catch (JSONException localJSONException1)
      {
        localJSONException1.printStackTrace();
      }
      this.attributes.put(str, localObject);
    }
    j = paramParcel.readInt();
    for (int k = 0; k < j; k++)
    {
      localObject = paramParcel.readString();
      JSONArray localJSONArray = null;
      try
      {
        localJSONArray = new JSONArray(paramParcel.readString());
      }
      catch (JSONException localJSONException2)
      {
        localJSONException2.printStackTrace();
      }
      this.attributes.put(localObject, localJSONArray);
    }
    Hashtable localHashtable = new Hashtable();
    paramParcel.readMap(localHashtable, null);
    this.attributes.putAll(localHashtable);
    this.from = ((EMContact)paramParcel.readParcelable(EMMessage.class.getClassLoader()));
    this.to = ((EMContact)paramParcel.readParcelable(EMMessage.class.getClassLoader()));
    this.body = ((MessageBody)paramParcel.readParcelable(EMMessage.class.getClassLoader()));
    this.chatType = ChatType.valueOf(paramParcel.readString());
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }

  public boolean isAcked()
  {
    return this.isAcked;
  }

  public void setAcked(boolean paramBoolean)
  {
    this.isAcked = paramBoolean;
  }

  public boolean isDelivered()
  {
    return this.isDelivered;
  }

  public void setDelivered(boolean paramBoolean)
  {
    this.isDelivered = paramBoolean;
  }

  public boolean isUnread()
  {
    return this.unread;
  }

  public void setUnread(boolean paramBoolean)
  {
    this.unread = paramBoolean;
  }

  public void setType(Type paramType)
  {
    this.type = paramType;
  }

  public boolean isListened()
  {
    return this.isListened;
  }

  public void setListened(boolean paramBoolean)
  {
    this.isListened = paramBoolean;
  }

  public String getUserName()
  {
    String str = "";
    if ((getFrom() != null) && (getFrom().equals(EMChatManager.getInstance().getCurrentUser())))
      str = getTo();
    else
      str = getFrom();
    return str;
  }

  public static enum ChatType
  {
    Chat, GroupChat;
  }

  public static enum Direct
  {
    SEND, RECEIVE;
  }

  public static enum Status
  {
    SUCCESS, FAIL, INPROGRESS, CREATE;
  }

  public static enum Type
  {
    TXT, IMAGE, VIDEO, LOCATION, VOICE, FILE, CMD;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMMessage
 * JD-Core Version:    0.6.1
 */